// pages/recommend/recommend.js
const http = require("../utils/http");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图列表
    bannerList:[],
    postList:[],
    lastid:0
  },

  goSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    http.get("/index/index").then(res=>{
      let {list} = res.banner;
      this.setData({
        bannerList:list,
        postList:res.posts.list,
        lastid:res.posts.lastid
      })
      // console.log(this.data.bannerList);
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    http.get(`/index/getIndexPosts/lastid/${this.data.lastid}`).then(res=>{
      let {lastid,list} = res;
      this.setData({
       lastid:lastid,
       postList:list.concat(this.data.postList)
      })
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   http.get(`/index/getIndexPosts/lastid/${this.data.lastid}`).then(res=>{
     let {lastid,list} = res;
     this.setData({
      lastid:lastid,
      postList:this.data.postList.concat(list)
     })
   })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})